import 'package:flutter/material.dart';
import 'package:osc/res/dimens.dart';

class CommonTabBar extends StatefulWidget {
  final List<Widget> tabs;
  final List<Widget> pages;
  final List<Widget> actions;
  final int selectTabIndex;
  final bool scrollable;

  const CommonTabBar({Key key, @required this.tabs, @required this.pages, this.selectTabIndex = 0, this.actions, this.scrollable = false}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new ContractListPageState(selectTabIndex);
  }
}

class ContractListPageState extends State<CommonTabBar> with SingleTickerProviderStateMixin {
  final int selectTabIndex;
  ContractListPageState(this.selectTabIndex);

  TabController tabController;

  @override
  void initState() {
    super.initState();
    // 创建Controller
    tabController = TabController(length: widget.tabs.length, vsync: this);
    if(selectTabIndex != 0){
      tabController.animateTo(selectTabIndex);
    }
  }

  @override
  void dispose() {
    super.dispose();
    tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
          child: AppBar(
            leading: null,//和automaticallyImplyLeading: false 同时使用可使title横向铺满
            automaticallyImplyLeading: false,
            actions: widget.actions,
            titleSpacing: 0,
            elevation: 1,
            title: TabBar(
                isScrollable: widget.scrollable,
                controller: tabController,
                indicatorWeight: 1.5,
                indicatorPadding: EdgeInsets.all(0),
                tabs: widget.tabs,
            ),
          ),
          preferredSize: Size.fromHeight(Dimens.view_tab_bar_height)
      ),
      body: TabBarView(
          controller: tabController,
          children: widget.pages,
      ),
    );
  }

}
